<template>
  <div class="pagination-container">
    <el-pagination
      :current-page.sync="currentPage"
      :page-sizes="pageSizes"
      :page-size.sync="pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
// 使用方案一或方案二的请求方式
import { get } from '@/api/request' // 或 import axios from 'axios'

export default {
  name: 'PaginationUrl',
  props: {
    url: {
      type: String,
      required: true
    },
    params: {
      type: Object,
      default: () => ({})
    },
    pageSizes: {
      type: Array,
      default: () => [10, 20, 30, 50]
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    }
  },
  data() {
    return {
      currentPage: 1,
      pageSize: this.pageSizes[0],
      total: 0
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData(callback) {
      const params = {
        ...this.params,
        page: this.currentPage - 1,
        size: this.pageSize
      }

      get(this.url, params).then(response => {
        const res = response.data
        if (callback) {
          callback(res)
        } else {
          this.$emit('update:data', res.content || res.records)
          this.total = res.totalElements || res.total
        }
      }).catch(err => {
        console.error('请求失败:', err)
        this.$emit('error', err)
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.loadData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.loadData()
    },
    refresh() {
      this.currentPage = 1
      this.loadData()
    }
  }
}
</script>

<style scoped>
.pagination-container {
  padding: 10px 0;
  text-align: right;
}
</style>
